﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Enterprisess;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Bedroomss
{
    public class ShowBedroomHander : IRequestHandler<ShowBedroomComand, Result<PageResultDto<BedroomDto>>>
    {
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        private readonly IBaseRepository<Bedrooms> bedroomsRepository;
        /// <summary>
        /// 栋舍分类信息仓储
        /// </summary>
        private readonly IBaseRepository<BedroomTypes> bedroomTypesRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bedroomsRepository"></param>
        /// <param name="bedroomTypesRepository"></param>
        public ShowBedroomHander(IBaseRepository<Bedrooms> bedroomsRepository, IBaseRepository<BedroomTypes> bedroomTypesRepository)
        {
            this.bedroomsRepository = bedroomsRepository;
            this.bedroomTypesRepository = bedroomTypesRepository;
        }

        /// <summary>
        /// 栋舍列表显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<BedroomDto>>> Handle(ShowBedroomComand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<BedroomDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var page = new PageResultDto<BedroomDto>() { List = new List<BedroomDto>(), PageCount = 0, TotalCount = 0 };

                var query = (from a in await bedroomsRepository.GetAll()
                             join b in await bedroomTypesRepository.GetAll()
                             on a.BedroomTypeId equals b.BedroomTypeId
                             select new BedroomDto()
                             {
                                 BedroomId = a.BedroomId,
                                 BedroomTypeId = a.BedroomTypeId,
                                 BedroomName = a.BedroomName,
                                 PersonName = a.PersonName,
                                 ProduceName = a.ProduceName,
                                 State = a.State,
                                 Notes = a.Notes,
                                 BedroomTypeName = b.BedroomTypeName,
                             }).ToList();
                //栋舍名称模糊查询
                if (!string.IsNullOrEmpty(request.BedroomName))
                {
                    query = query.Where(x => x.BedroomName.Contains(request.BedroomName)).ToList();
                }
                //栋舍类型精确查
                if (request.BedroomTypeId != 0)
                {
                    query = query.Where(x => x.BedroomTypeId == request.BedroomTypeId).ToList();
                }
                //负责人模糊查
                if (!string.IsNullOrEmpty(request.PersonName))
                {
                    query = query.Where(x => x.PersonName.Contains(request.PersonName)).ToList();
                }
                page.TotalCount = query.Count();
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
                page.List = query.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                if (query != null)
                {
                    result.Data = page;
                    result.Code = (int)CommonErrorCode.Success;
                }

            }
            catch (Exception ex)
            {
                result.Msg = "获取栋舍异常" + ex.Message;
            }
            return result;
        }
    }
}
